/*
 * StreetModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import android.text.TextUtils;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Brand;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.recyclertype.DoubleBrand;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.request.QueryBrandNewGoodsRequest;
import com.yidiandao.business.data.request.QueryBrandPriceGoodsRequest;
import com.yidiandao.business.data.request.QueryBrandSalesGoodsRequest;
import com.yidiandao.business.data.response.QueryBrandStreetResponse;
import com.yidiandao.business.data.response.QueryGoodsListResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌街Model
 */
public class StreetModel implements IStreetModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DoubleGoods> mGoodsGridList = new ArrayList<>();

    private List<DoubleBrand> mBrandList = new ArrayList<>();

    private List<Goods> mGoodsList = new ArrayList<>();

    private int brandStreetPager, newGoodsListPager, priceGoodsListPager, salesGoodsListPager;

    @Override
    public void loadBrandStreet(
            final boolean isRefreshing, String tag, final OnLoadBranStreetListener listener) {

        if (isRefreshing) {
            brandStreetPager = 1;
        }

        PagerRequest request = new PagerRequest();
        request.setUrl(Urls.HOME_BRAND_STREET);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(brandStreetPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryBrandStreetResponse.class,
                new NetListener<QueryBrandStreetResponse>() {
                    @Override
                    public void onSuccess(QueryBrandStreetResponse response) {

                        brandStreetPager++;

                        List<Brand> brandList = response.getData().getBrandList();

                        if (isRefreshing) {
                            mBrandList.clear();
                        }

                        for (int i = 0; i < brandList.size(); i += 2) {
                            Brand brand1 = null;
                            Brand brand2 = null;
                            try {
                                brand1 = brandList.get(i);
                                brand2 = brandList.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mBrandList.add(new DoubleBrand(brand1, brand2));
                        }

                        if (listener != null) {
                            listener.onSuccess(mBrandList, brandList.size() == 0, response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadStreetNewGoodsList(
            final boolean isRefreshing,
            String brandId,
            String supplierId,
            String tag,
            final OnLoadGoodsListener listener) {

        if (isRefreshing) {
            newGoodsListPager = 1;
        }

        QueryBrandNewGoodsRequest request = new QueryBrandNewGoodsRequest();
        request.setUrl(Urls.BRAND_GOODS_LIST);
        request.setBrandId(brandId);
        if (!TextUtils.isEmpty(supplierId)) {
            request.setSupplierId(supplierId);
        }
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(newGoodsListPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        newGoodsListPager++;

                        List<Goods> goodsLines = response.getData().getGoods();

                        if (isRefreshing) {
                            mGoodsGridList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mGoodsList,
                                    mGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadStreetPriceGoodsList(
            final boolean isRefreshing,
            String brandId,
            String supplierId,
            boolean isSortUp,
            String tag,
            final OnLoadGoodsListener listener) {

        if (isRefreshing) {
            priceGoodsListPager = 1;
        }

        QueryBrandPriceGoodsRequest request = new QueryBrandPriceGoodsRequest();
        request.setUrl(Urls.BRAND_GOODS_LIST);
        request.setBrandId(brandId);
        if (!TextUtils.isEmpty(supplierId)) {
            request.setSupplierId(supplierId);
        }
        request.setTag(tag);
        request.setPrice(isSortUp
                ? QueryBrandPriceGoodsRequest.SORT_UP
                : QueryBrandPriceGoodsRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(priceGoodsListPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        priceGoodsListPager++;

                        List<Goods> goodsLines = response.getData().getGoods();

                        if (isRefreshing) {
                            mGoodsGridList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mGoodsList,
                                    mGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadStreetSalesGoodsList(
            final boolean isRefreshing,
            String brandId,
            String supplierId,
            String tag,
            final OnLoadGoodsListener listener) {
        if (isRefreshing) {
            salesGoodsListPager = 1;
        }

        QueryBrandSalesGoodsRequest request = new QueryBrandSalesGoodsRequest();
        request.setUrl(Urls.BRAND_GOODS_LIST);
        request.setBrandId(brandId);
        if (!TextUtils.isEmpty(supplierId)) {
            request.setSupplierId(supplierId);
        }
        request.setTag(tag);
        request.setSalesNum(QueryBrandSalesGoodsRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(salesGoodsListPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        salesGoodsListPager++;

                        List<Goods> goodsLines = response.getData().getGoods();

                        if (isRefreshing) {
                            mGoodsGridList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mGoodsGridList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mGoodsList,
                                    mGoodsGridList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }
}
